import axios from 'axios'

async function request<T>(
  method: string,
  url: string,
  data?: any,
  config?: any,
) {
  let res: T
  if (method === 'POST') {
    res = await axios.post(url, data, config)
  }
  if (method === 'GET') {
    res = await axios.get(url, config)
  }
  if (method === 'PUT') {
    res = await axios.put(url, data, config)
  }
  if (method === 'DELETE') {
    res = await axios.delete(url, config)
  }
  // @ts-ignore
  return res
}

export function get<T>(url: string, config: any) {
  return request<T>('GET', url, '', config)
}

export function post<T>(url: string, data?: any, config?: any) {
  return request<T>('POST', url, data, config)
}

export function put<T>(url: string, data?: any, config?: any) {
  return request<T>('PUT', url, data, config)
}

export function deleteApi<T>(url: string, config?: any) {
  return request<T>('DELETE', url, '', config)
}
